const config = require('../config'); // 引入配置文件
const myMd5 = require('../utils/md5password'); // 引入加密模块
const UsrDao = require('../dao/userDao'); // 引入数据库

exports.test = (req, res) => {
    res.send('Hello World!');
}

// 获取版本号
exports.getVersion = (req, res) => {
    const verNum = config.CLIENT_VERSION;
    res.send({ verNum: verNum });
}

// 数据库
exports.getDB = (req, res) => {
    const user = UsrDao.getUserById(1); // 查询用户
    res.send(user);
}

// 登录
exports.login = (req, res) => {
    const { account, password } = req.query; // 获取请求参数
    if (!account || !password) {
        res.send({ code: 1, msg: '参数错误' });
    } else {
        UsrDao.checkUserAccount(account).then((result) => {
            if (result) {
                UsrDao.checkUserPassword(account, password).then((result) => {
                    if (result) {
                        const sign = myMd5.md5(result.userid + req.ip + config.ACCOUNT_KEY);
                        res.send({ code: 0, msg: '登录成功', sign: sign, userInfo: result });
                    } else {
                        res.send({ code: 1, msg: '密码错误' });
                    }
                })
            } else {
                res.send({ code: 1, msg: '账号不存在' });
            }
        })
    }
}

// 注册用户
exports.register = (req, res) => {
    const { account, password } = req.query; // 获取请求参数    
    if (!account || !password) {
        res.send({ code: 1, msg: '参数错误' });
    } else {
        UsrDao.checkUserAccount(account).then((result) => {
            if (result) {
                res.send({ code: 1, msg: '用户名已存在' });
            } else {
                UsrDao.registerUser(account, password).then((result) => {
                    if (result) {
                        // 封装密钥
                        const key = myMd5.md5(account + req.ip + config.ACCOUNT_KEY);
                        res.send({ code: 0, msg: '注册成功', key: key });
                    } else {
                        res.send({ code: 1, msg: '注册失败' });
                    }
                });
            }
        })
    }
}

// 创建角色
exports.createRole = (req, res) => {
    const { account, sign, sex, username } = req.query;
    if (!account || !sign || !sex || !username) {
        res.send({ code: 1, msg: '参数错误' });
    } else {
        // 验证昵称是否存在
        UsrDao.checkUsername(username).then((result) => {
            if (result) {
                res.send({ code: 1, msg: '昵称已存在' });
            } else {
                UsrDao.createRole(account, sex, username).then((result) => {
                    if (result) {
                        res.send({ code: 0, msg: '创建角色成功' });
                    } else {
                        res.send({ code: 1, msg: '创建角色失败' });
                    }
                });
            }
        })
    }
}

// 获取建筑物信息
exports.getBuildingInfo = (req, res) => {
    UsrDao.getBuildingInfo().then((result) => {
        if (result) {
            res.send({ code: 0, msg: '获取建筑物信息成功', data: result });
        } else {
            res.send({ code: 1, msg: '获取建筑物信息失败' });
        }
    })
}